#include <gdk/gdkkeysyms.h>

#include "window.h"
#include "wspace_switcher.h"
#include "launchers_pad.h"



/**
 * window_new:
 *
 * Create new windows. Window is a GtkWindow widget.
 * Decorate him and create launcher pad and wokspace switcher.
 *
 * Return value: a new GtkWidget.
 **/
GtkWidget * window_new ()
{
    GtkWidget* win;
    GtkWidget* vbox;
    GtkWidget* launchers_table;
    GtkWidget* wspaces_box;
//    GtkWidget* frame;


    /* Create the main window */
    win = gtk_window_new (GTK_WINDOW_TOPLEVEL);
    gtk_container_set_border_width (GTK_CONTAINER (win), 8);
    gtk_window_set_title (GTK_WINDOW (win), "gtk-speed-dial");
    gtk_window_set_position (GTK_WINDOW (win), GTK_WIN_POS_CENTER);
    gtk_widget_realize (win);
    //gtk_window_set_decorated (GTK_WINDOW (win), FALSE);
    //gtk_window_set_keep_below(GTK_WINDOW (win), TRUE);
    gtk_window_stick (GTK_WINDOW (win));
    g_signal_connect (win, "destroy", gtk_main_quit, NULL);

    // decorate
    // gdk_window_shape_combine_mask ()
    //g_signal_connect (win, "expose-event", G_CALLBACK (window_on_expose_event), NULL);
    //gdk_draw_lines ();

    // close on Escape
    g_signal_connect (win, "key-release-event", G_CALLBACK (window_on_key_release_event), NULL);


    /* Create a vertical box with buttons */
    vbox = gtk_vbox_new (FALSE, 2);
    gtk_box_set_spacing (GTK_BOX(vbox), 4);
    gtk_container_add (GTK_CONTAINER (win), vbox);

    // workspaces
    wspaces_box = wspace_switcher_new ();
    gtk_box_pack_end (GTK_BOX(vbox), wspaces_box, FALSE, TRUE, 0);

    // launchers
    launchers_table = launchers_pad_new (win);
    // border
    //frame = gtk_frame_new("");
    //gtk_frame_set_shadow_type (GTK_FRAME(frame), GTK_SHADOW_IN);
    //gtk_container_set_border_width (GTK_CONTAINER(frame), 2);
    //gtk_container_add (GTK_CONTAINER(frame), launchers_table);
    gtk_box_pack_start (GTK_BOX (vbox), launchers_table, TRUE, TRUE, 0);

    wspace_switcher_sync ();

    return win;
}


gboolean window_on_expose_event (
    GtkWidget*      widget,
    GdkEventExpose* event,
    gpointer        user_data)
{
    gint width;
    gint height;
    gint rect_x;
    gint rect_y;
    gint rect_w;
    gint rect_h;


    // get window size
    // calc rectangle coords
    // draw rectangle

    width = GTK_WIDGET (widget)->allocation.width;
    height = GTK_WIDGET (widget)->allocation.height;

    rect_x = 2;
    rect_y = 2;
    rect_w = width - 2 * rect_x;
    rect_h = height - 2 * rect_y;
/*
    g_message ("width: %d\n", width);
    g_message ("height: %d\n", height);
*/
    gdk_draw_rectangle (widget->window,
                          widget->style->black_gc,
                          TRUE,
                          rect_x,
                          rect_y,
                          rect_w,
                          rect_h);

    //gdk_draw_lines (GDK_DRAWABLE (widget->window), self->gc);


    return FALSE;
}


/**
 * window_on_key_release_event:
 *   @widget:  GtkWidget *
 *   @event:  GdkEventKey *
 *   @user_data:  gpointer
 *
 * Quit application when Escape pressed
 *
 * Return value: TRUE - stop other event handlers,
 *               FALSE - contine event processing
 **/
gboolean window_on_key_release_event (GtkWidget   *widget,
                                      GdkEventKey *event,
                                      gpointer     user_data)
{
    if (event->type == GDK_KEY_RELEASE)
        if (event->keyval == GDK_Escape)
            gtk_main_quit();

    return FALSE;
}
